/*
 * Copyright 2011 derek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aocore.map;

import com.aocore.awesomo.Player;
import com.aocore.d2data.AreaLevel;
import com.aocore.d2data.GameDifficulty;
import com.aocore.map.path.AOPathFinder;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author derek
 */
public class GameMap
{
    private final int mapId;
    private final GameDifficulty difficulty;
    private String sessionId;

    private Map[] maps = new Map[145];//TODO: are there 145 maps?

    // need tree from http://newd2event.net/index.php?id=maps/act1
    // need to know which areas have wp's

    //[Rogue Encampment, Blood Moor, Cold Plains, Burial Grounds, Stony Field]
    //[Dark Wood, Black Marsh, Tamoe Highland, Monastery Gate, Outer Cloister, Barracks]
    //[Inner Cloister, Cathedral]
    //[Lut Gholein, Rocky Waste, Dry Hills, Far Oasis, Lost City, Valley of Snakes]
    //[Kurast Docktown, Spider Forest, Great Marsh, Flayer Jungle, Lower Kurast, Kurast Bazaar, Upper Kurast, Kurast Causeway, Travincal]
    //[The Pandemonium Fortress, Outer Steppes, Plains of Despair, City of the Damned]
    //[River of Flame, Chaos Sanctum]
    //[Harrogath, Bloody Foothills, Rigid Highlands, Arreat Plateau]

    public GameMap(int mapId, GameDifficulty difficulty) throws IOException
    {
        this.mapId = mapId;
        this.difficulty = difficulty;

        sessionId = MapClient.getSession(mapId, difficulty);
    }

    public void loadMap(AreaLevel area)
    {
        try
        {
            for (Map map : maps)
            {
                if (area.equals(map.getArea()))
                {
                    return;
                }
            }

            MapClient.loadMap(sessionId, area);
        }
        catch (IOException e)
        {
            e.printStackTrace(System.err);
        }
    }

    public AreaLevel getArea(Player player)
    {
        for (Map map : maps)
        {
            if (map.isValid(player.getX(), player.getY()))
            {
                return map.getArea();
            }
        }

        return null;
    }

    public Map getMap(AreaLevel area)
    {
        try
        {
            if (maps[area.getId()] == null)
            {
                maps[area.getId()] = MapClient.getMap(sessionId, area);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace(System.err);
        }

        return maps[area.getId()];
    }

    public List<MapLocation> path(AreaLevel fromArea, int fromX, int fromY, AreaLevel toArea, int toX, int toY)
    {
        // were going to need these for sure...
        loadMap(fromArea);
        loadMap(toArea);

        AOPathFinder aoPathFinder = new AOPathFinder(this);
        return aoPathFinder.path(fromArea, fromX, fromY, toArea, toX, toY);
    }
}
